package cn.school.controller.workmgr;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.educational.entity.TeachingMaterialCollect;
import cn.school.workmgr.entity.HygieneShcool;
import cn.school.workmgr.service.HygieneShcoolService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: HygieneShcoolController
 * @author: fangjin.tao
 * @date: 2022/12/9
 **/
@RestController
@Api(tags = "HygieneShcoolController => 校园卫生管理")
@RequestMapping("/workmgr/hygieneShcool")
public class HygieneShcoolController {
	
	@Autowired
	private HygieneShcoolService hygieneShcoolService;
	
	
	@ApiOperation("【校园卫生登记】获取列表")
	@GetMapping("/list")
	public CommonResult<CommonPage<HygieneShcool>> list(@RequestParam Map<String, Object> para) {
		// 条件获取
		int pageNum = Integer.parseInt((String) para.get("pageNum"));
		int pageSize = Integer.parseInt((String) para.get("pageSize"));
		
		String hyshDate = (String) para.get("hyshDate"); // 卫生日期
		String hyshFlag = (String) para.get("hyshFlag"); // 卫生标记
		String createByName = (String) para.get("createByName"); // 创建人名称
		
		Page<HygieneShcool> page = new Page<>();
		page.setSize(pageSize);
		page.setCurrent(pageNum);
		
		// queryWrapper查询设置
		LambdaQueryWrapper<HygieneShcool> queryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotEmpty(hyshDate)) {
			// 具体日期
			queryWrapper.ge(HygieneShcool::getHyshDate, DateUtil.parse(hyshDate + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
			queryWrapper.le(HygieneShcool::getHyshDate, DateUtil.parse(hyshDate + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		}
		
		if (StrUtil.isNotEmpty(hyshFlag)) {
			queryWrapper.eq(HygieneShcool::getHyshFlag, hyshFlag);
		}
		if (StrUtil.isNotEmpty(createByName)) {
			queryWrapper.eq(HygieneShcool::getCreateByName, createByName);
		}
		//租户
		queryWrapper.eq(HygieneShcool::getTenantId, SysUserUtils.getSysUser().getTenantId());
		// 排序
		queryWrapper.orderByDesc(HygieneShcool::getCreateTime);
		
		// 查询
		IPage<HygieneShcool> iPage = this.hygieneShcoolService.page(page, queryWrapper);
		
		return CommonResult.success(CommonPage.restPage(iPage));
	}
	
	@ApiOperation("【校园卫生登记】获取详情")
	@GetMapping("/info/{id}")
	public CommonResult<HygieneShcool> getInfoById(@PathVariable Long id) {
		HygieneShcool hygieneShcool = this.hygieneShcoolService.getById(id);
		if (hygieneShcool == null) {
			throw new ApiException("领用记录不存在，请刷新页面");
		}
		return CommonResult.success(hygieneShcool);
	}
	
	@ApiOperation("【校园卫生登记】添加")
	@PostMapping("/addOrUpdate")
	@Transactional
	public CommonResult<HygieneShcool> add(@RequestBody HygieneShcool hygieneShcool) {
		boolean isAddSuccess = false;
		if (hygieneShcool.getId() == null) {
			if (StrUtil.isNotEmpty(SysUserUtils.getSysUser().getSysuName())) {
				hygieneShcool.setCreateByName(SysUserUtils.getSysUser().getSysuName());
			} else {
				hygieneShcool.setCreateByName(SysUserUtils.getSysUser().getUsername());
			}
			hygieneShcool.setCreateTime(Calendar.getInstance().getTime());
			hygieneShcool.setModifyTime(Calendar.getInstance().getTime());
			hygieneShcool.setCreateBy(SysUserUtils.getSysUser().getId());
			isAddSuccess = this.hygieneShcoolService.save(hygieneShcool);
		} else {
			hygieneShcool.setModifyTime(Calendar.getInstance().getTime());
			hygieneShcool.setModifyBy(SysUserUtils.getSysUser().getId());
			isAddSuccess = this.hygieneShcoolService.updateById(hygieneShcool);
		}
		if (isAddSuccess) {
			return CommonResult.success();
		} else {
			return CommonResult.failed();
		}
	}
	
	@ApiOperation("【校园卫生登记】删除")
	@DeleteMapping("/deleteByIds/{ids}")
	public CommonResult<TeachingMaterialCollect> delete(@PathVariable Long[] ids) {
		List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
		boolean isDeleteSuccess = this.hygieneShcoolService.removeByIds(idList);
		if (isDeleteSuccess) {
			return CommonResult.success();
		} else {
			return CommonResult.failed();
		}
	}
}
